package com.ciko.code.base;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.ActionBar.LayoutParams;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ciko.code.constant.Constants;
import com.ciko.code.http.BaseRequest;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.HttpResponseListener;
import com.ciko.code.utils.AppUtils;
import com.ciko.code.utils.LogUtils;
import com.ciko.code.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.internal.Primitives;
import com.mywork.R;


public abstract class BaseActicity
        extends
        AppCompatActivity
        implements
        OnClickListener,
        HttpResponseListener {

    private final String TAG = this.getClass().getSimpleName();

    protected LinearLayout ll_root;

    private View common_nav;

    private View rect_imgNavBack;
    private View rect_imgNavRight;

    private TextView tv_title;

    private ViewStub common_nav_left;
    private ViewStub common_nav_right;
    private ViewStub common_nav_title;

    private FrameLayout fl_content;

    private OnNavRightClickListener onNavRightClickListener;
    protected ProgressDialog progressDialog, progressDialog2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LogUtils.v(TAG, "onCreate()");
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.layout_root_base);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("请稍候...");
        progressDialog.setCancelable(false);
        progressDialog2 = progressDialog;
        progressDialog2.setMessage("加载中...");
        ll_root = (LinearLayout) super.findViewById(R.id.ll_root);
        common_nav = super.findViewById(R.id.common_nav);

        common_nav_left = (ViewStub) super.findViewById(R.id.common_nav_left);
        common_nav_right = (ViewStub) super.findViewById(R.id.common_nav_right);
        common_nav_title = (ViewStub) super.findViewById(R.id.common_nav_title);

        fl_content = (FrameLayout) super.findViewById(R.id.fl_content);

        //        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        //            setTranslucentStatus(true);
        //            SystemBarTintManager tintManager = new SystemBarTintManager(this);
        //            tintManager.setStatusBarTintEnabled(true);
        //            tintManager.setStatusBarTintResource(R.color.nav_bg_root);
        //            SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
        //            ll_root.setPadding(0, config.getStatusBarHeight(), 0, 0);
        //        }

        getIntenData();
        initTopBar();
        initLayout();
        initView();
        initAdapter();
        initListener();
        initDates();
        initHttpRequest();

    }

    public void setContentView(int layoutResID) {
        LogUtils.v(TAG, "setContentView()");
        fl_content.addView(getLayoutInflater().inflate(layoutResID, null), LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    protected abstract void initTopBar();

    protected abstract void initLayout();

    protected abstract void initView();

    protected abstract void initAdapter();

    protected abstract void initDates();

    protected abstract void initListener();

    protected void initHttpRequest() {

    }

    protected void getIntenData() {

    }

    public <T extends View> T findViewByID(int id) {
        return (T) fl_content.findViewById(id);
    }

    protected void unableNav() {
        common_nav.setVisibility(View.GONE);
    }

    protected void enableNavLeftImg() {
        LogUtils.v(TAG, "enableNavLeftImg()");

        common_nav_left.setLayoutResource(R.layout.layout_nav_left_img);
        rect_imgNavBack = common_nav_left.inflate();
        rect_imgNavBack.setOnClickListener(this);
    }

    protected void enableNavRightImg(int imgResID) {
        LogUtils.v(TAG, "enableNavRightImg()");

        common_nav_right.setLayoutResource(R.layout.layout_nav_right_img);
        rect_imgNavRight = common_nav_right.inflate();
        ImageView img = (ImageView) rect_imgNavRight.findViewById(R.id.iv_imgNavRight);
        img.setImageResource(imgResID);
        img.setVisibility(View.VISIBLE);
        rect_imgNavRight.setOnClickListener(this);
    }

    protected void enableNavRightIv(String string) {
        LogUtils.v(TAG, "enableNavRightImg()");

        common_nav_right.setLayoutResource(R.layout.layout_nav_right_img);
        rect_imgNavRight = common_nav_right.inflate();
        TextView tv = (TextView) rect_imgNavRight.findViewById(R.id.iv_tvNavRight);
        tv.setText(string);
        tv.setVisibility(View.VISIBLE);
        rect_imgNavRight.setOnClickListener(this);
    }

    public void setTitle(CharSequence title) {
        LogUtils.v(TAG, "setTitle()");
        if (tv_title == null) {
            common_nav_title.setLayoutResource(R.layout.layout_nav_title);
            tv_title = (TextView) common_nav_title.inflate();
        }
        tv_title.setText(title);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        LogUtils.v(TAG, "setTranslucentStatus()");

        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    public void onClick(View v) {
        LogUtils.v(TAG, "onClick()");
        if (v == rect_imgNavBack) {
            finish();
        } else if (v == rect_imgNavRight) {
            if (onNavRightClickListener != null) {
                onNavRightClickListener.onNavRightClick();
            }
        }
        //        switch (v.getId()) {
        //            case R.id.rect_imgNavBack:
        //                finish();
        //                break;
        //            case R.id.rect_imgNavRight:
        //                if (onNavRightClickListener != null) {
        //                    onNavRightClickListener.onNavRightClick();
        //                }
        //                break;
        //        }

    }

    protected void showToast(CharSequence msg) {
        LogUtils.v(TAG, "showToast()");
        ToastUtils.showShort(msg);
    }

    protected void intent2Acticity(Class<?> cls) {
        Intent intent = new Intent(BaseActicity.this, cls);
        startActivity(intent);
    }

    protected void intent3Acticity(Class<?> cls) {
        Intent intent = new Intent(BaseActicity.this, cls);
        startActivityForResult(intent, 1);
    }

    protected void logV(String msg) {
        LogUtils.v(TAG, msg);
    }

    protected void logV(int msg) {
        LogUtils.v(TAG, msg + "");
    }

    protected void logD(String msg) {
        LogUtils.d(TAG, msg);
    }

    protected void logD(int msg) {
        LogUtils.d(TAG, msg + "");
    }

    protected void logI(String msg) {
        LogUtils.i(TAG, msg);
    }

    protected void logI(int msg) {
        LogUtils.i(TAG, msg + "");
    }

    protected void logW(String msg) {
        LogUtils.w(TAG, msg);
    }

    protected void logW(int msg) {
        LogUtils.w(TAG, msg + "");
    }

    protected void logE(String msg) {
        LogUtils.e(TAG, msg);
    }

    protected void logE(int msg) {
        LogUtils.e(TAG, msg + "");
    }

    protected void toast(Object msg) {
        ToastUtils.showShort(msg);
    }

    public interface OnNavRightClickListener {
        public void onNavRightClick();
    }

    public void setOnNavRightClickListener(OnNavRightClickListener onNavRightClickListener) {
        this.onNavRightClickListener = onNavRightClickListener;
    }

    protected <T> T getGsonData(String json, Class<T> classOfT) {
        Object object = new Gson().fromJson(json, classOfT);
        return Primitives.wrap(classOfT).cast(object);
    }

    @Override
    public void onRequestStart(String tag) {

    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {

        BaseResponse obj = (BaseResponse) response;
        Log.e("Success", obj.getResultCode() + "");
        if (obj.getResultCode() == 1003) {
            Intent intent = new Intent();
            intent.setAction(Constants.EXCEPTION_LOGIN_STRING);
            intent.putExtra("json", "1003");
            intent.putExtra("packageName", AppUtils.getPackageName(this));
            sendBroadcast(intent);

        }

    }

    @Override
    public void onResponseFail(String tag, int resultCode) {

        if (resultCode == 1003) {
            Intent intent = new Intent();
            intent.setAction(Constants.EXCEPTION_LOGIN_STRING);
            intent.putExtra("json", "1003");
            intent.putExtra("packageName", AppUtils.getPackageName(this));
            sendBroadcast(intent);

        }
    }

    public void loadImageUrl(ImageView imageView, String imgUrl) {
        Glide.with(this).load(imgUrl).placeholder(R.drawable.shape_rect_gray).into(imageView);
    }

    protected void doRequestHttp(BaseRequest request) {
        HttpClient.getInstance(this).post(request, this);
    }

}
